﻿using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Zhao.Common.Helper;
using Zhao.IServer.Events;
using Zhao.Models.DTO;
using Zhao.Models.Entity;
using Zhao.Repository.Base;
using Zhao.Repository.UnitOfWork;

namespace Zhao.Service.Events
{
    public class ActivityMembersServer : BaseService<ActivityMembers>, IActivityMembersServer
    {
        public ActivityMembersServer(IBaseRepository<ActivityMembers> baseDal, IUnitOfWork unitOfWork) : base(baseDal, unitOfWork)
        {
        }

        public async Task<PageModel<ActivityMembersDto>> QueryActivityMembersPage(Expression<Func<ActivityMembers,Users,Activities, bool>> expressionable, int pageIndex,int pageSize)
        {
            //获取总条数
            RefAsync<int> totalCount = 0;

            var list=await _Client.Queryable<ActivityMembers>()
                .LeftJoin<Users>((a,b)=>a.UserId==b.UserID)
                .LeftJoin<Activities>((a,b,c)=>a.ActivityId==c.ActivityID)
                .WhereIF(expressionable != null, expressionable)
                .Select((a,b)=>new ActivityMembersDto
                {
                    Id = a.Id,
                    Name=b.Username,
                    CreateTime=a.CreateTime,
                    Status=a.Status,
                    MaleNumber=a.MaleNumber,
                    FemaleNumber=a.FemaleNumber,
                }).ToPageListAsync(pageIndex, pageSize, totalCount).ConfigureAwait(false);

            int totalPage = Math.Ceiling(totalCount.ObjToDecimal() / pageSize.ObjToDecimal()).ObjToInt();

            return new PageModel<ActivityMembersDto>() { count = totalCount, PageCount = totalPage, PageIndex = pageIndex, PageSize = pageSize, data = list, code = totalCount == 0 ? -1 : 0, msg = totalCount == 0 ? "未查询到任何数据" : "" };
        }
    }
}
